package top.gytf.distools.lifecycle;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * 通用逻辑的生命周期
 */
@Slf4j
@Getter
public abstract class AbstractLifecycle implements Lifecycle {
    private volatile boolean initialized = false;
    private volatile boolean destroyed = false;

    /**
     * 执行组件管理器独有的初始化逻辑
     */
    protected void doInitialize() {
        // do noting
    }

    @Override
    public final synchronized void initialize() {
        if (isInitialized()) {
            log.warn("{} 已初始化", getName());
            return;
        }
        doInitialize();
        this.initialized = true;
    }

    /**
     * 执行组件管理器独有的销毁逻辑
     */
    protected void doDestroy() {
        // do noting
    }

    @Override
    public final synchronized void destroy() {
        if (isDestroyed()) {
            log.warn("{} 已销毁", getName());
            return;
        }
        doDestroy();
        this.destroyed = true;
    }
}
